import pymysql,logging
class Db_Util():
    @classmethod
    def conn_conn(cls):
        #2.建立连接，数据库连接对象 cancan
        cls.con = pymysql.connect(host="127.0.0.1",port=3306,user="root",password="root",database="ihrm",charset="utf8")
        logging.info('数据库连接成功')
        #3.获取游标,创建一对光标对象
        cls.cursor = cls.con.cursor()
        return cls.cursor
    @classmethod
    def clos_conn(cls):
        #6.关闭游标
        cls.cursor.close()
        #7.关闭连接
        cls.con.close()
        logging.info('数据库关闭成功')
    @classmethod
    def excuate_sql(cls,sql):
        try:
            cls.cursor.execute(sql)
            cls.con.commit()
            logging.info('测试数据删除成功')
        except:
            logging.info('测试数据不存在，无需删除')
# #4.执行sql查询语句
# # 查询
# cursor.execute("select * from bs_user where mobile=13800000001;")
# # 删除
# cursor.execute("delete from bs_user where mobile=13882638464")
# #提交
# con.commit()
# #5.获取结果
# res = cursor.fetchall()

